/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.User;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MUserService;
import org.cx1.web.controller.action.UserAction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class SignupController extends BaseController {
    
    //private IUserService userService = new MUserService();
    private IUserService userService = new DUserService();
    
    public SignupController() {
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();        
        
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        if (ServletFileUpload.isMultipartContent(request)) {
            parameterMap = this.getMultipartFormParams(request);
        } 
        else {
            parameterMap = this.getFormParams(request);
        }
        
        Map<String, String> formfields = (Map<String, String>) parameterMap.get("formfields");
        int action = StringUtils.isEmpty(formfields.get("a")) ? 0 : Integer.parseInt(formfields.get("a"));
        
        switch (action) {
            default: {
                signup(mav, map, request);
                break;
            }
        }  
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        mav.addObject("model", map);
        return mav;
    }
    
    private void signup(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
                        
            String userTypeID = request.getParameter("userTypeID");
            
            String brokerID = request.getParameter("brokerID");
            String gmailID = request.getParameter("gmailID");
            String yahooID = request.getParameter("yahooID");
            String facebookID = request.getParameter("facebookID");
            
            String email = request.getParameter("email");
            String password = request.getParameter("password");
                
            String firstName = StringUtils.isEmpty(request.getParameter("firstName")) ? "" : request.getParameter("firstName");
            String lastName = StringUtils.isEmpty(request.getParameter("lastName")) ? "" : request.getParameter("lastName");
                
            String title = request.getParameter("title");            
            String description = request.getParameter("description");            
            String notes = request.getParameter("notes");
            
            String primaryPhotoID = request.getParameter("primaryPhotoID");            
            String photoIDs = request.getParameter("photoIDs");        
            
            String contactIDs = request.getParameter("contactIDs");            
            String companyIDs = request.getParameter("companyIDs");
                       
            User user = new User();
            user.setUserTypeID(userTypeID);
            
            user.setBrokerID(brokerID);
            user.setGmailID(gmailID);
            user.setYahooID(yahooID);
            user.setFacebookID(facebookID);
            
            user.setEmail(email);
            user.setPassword(password);
            
            user.setFirstName(firstName);
            user.setLastName(lastName);
            user.setName(firstName + " " + lastName);
            
            user.setTitle(title);
            user.setDescription(description);
            user.setNotes(notes);
            
            user.setPrimaryPhotoID(primaryPhotoID);
            user.setPhotoIDs(StringUtils.isEmpty(photoIDs) ? null : Arrays.asList(photoIDs.split("\\s*,\\s*")));
            
            user.setContactIDs(StringUtils.isEmpty(contactIDs) ? null : Arrays.asList(contactIDs.split("\\s*,\\s*")));
            user.setCompanyIDs(StringUtils.isEmpty(companyIDs) ? null : Arrays.asList(companyIDs.split("\\s*,\\s*")));

            ServiceResponse<String> response = userService.create(user);
            if (response.isSuccessful() && !StringUtils.isEmpty(response.getResponse())) {
                //mav.setViewName("signupok");
                mav.setView(new RedirectView("login.htm"));
            }
            else {
                //CX: display error messages
                map.put("message", "Oops, something went wrong: " + response.getMessage());
                map.put("user", user);
            }
        }
        else {
            //mav.setViewName("signup");
        }
        
        mav.setView(new RedirectView("invite.htm"));
    }
}